package com.wv.api.service.impl;

import cn.dev33.satoken.util.SaResult;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.wv.api.mapper.SysRoleDao;
import com.wv.api.service.ISysRoleService;
import com.wv.common.result.HttpCodeEnum;
import com.wv.common.result.PageResponseResult;
import com.wv.common.result.Result;
import com.wv.model.system.SysRole;
import com.wv.model.system.condition.SysRoleCondition;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

@Service
public class SysRoleServiceImpl implements ISysRoleService {

    @Autowired
    private SysRoleDao sysRoleDao;

    @Override
    public Result add(SysRole sysRole) {
        sysRole.setCreateTime(LocalDateTime.now());
        int insert = sysRoleDao.insert(sysRole);
        if (insert <= 0)return Result.error(HttpCodeEnum.ERROR);
        return Result.ok();
    }

    @Override
    public Result remove(String[] ids) {
        int i = sysRoleDao.deleteBatchIds(Arrays.asList(ids));
        return Result.ok();
    }

    @Override
    public PageResponseResult findPageList(SysRoleCondition sysRoleCondition) {
        sysRoleCondition.checkParam();
        PageHelper.startPage(sysRoleCondition.getPage(),sysRoleCondition.getSize());
        List<SysRole> sysRoles = sysRoleDao.selectPageList(sysRoleCondition);
        PageResponseResult build = PageResponseResult.builder().size(sysRoleCondition.getSize())
                .currentPage(sysRoleCondition.getPage())
                .total(0L).build();
        build.setData(sysRoles);
        return build;
    }


    @Override
    public Result update(SysRole sysRole) {
        sysRole.setUpdateTime(LocalDateTime.now());
        int update = sysRoleDao.updateById(sysRole);
        if (update <= 0) return Result.error(HttpCodeEnum.ERROR);
        return Result.ok();
    }

    @Override
    public Result findAll() {
        return Result.ok(sysRoleDao.selectList(null));
    }
}
